﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Data.Sql;

public partial class Boss_RestInf : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            fullFillData();
        }
    }

    private void fullFillData()
    {
        string connectionString = WebConfigurationManager.ConnectionStrings["ConnectionStrBoss"].ConnectionString;
        using (SqlConnection conn = new SqlConnection(connectionString))
        {
            try
            {
                conn.Open();

                String sqlString = "select * from Boss_Rest";
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sqlString;
                    SqlDataReader dr = cmd.ExecuteReader();
                    dr.Read();

                    RnoLab.Text = dr["Rno"].ToString();
                    RnameTex.Text = dr["Rname"].ToString();
                    PhoneTex.Text = dr["Rphone"].ToString();
                    AdrTex.Text = dr["Radr"].ToString();
                    BriefTex.Text = dr["Rbrief"].ToString();
                    dr.Close();
                }

            }
            catch (Exception ez)
            {
                Response.Write(@"<b>异常信息:</b>" + ez.Message);
            }
            finally
            {
                conn.Close();
            }
        }
    }
    protected void ResetBut_Click(object sender, EventArgs e)
    {
        fullFillData();
    }
    protected void UpdateBut_Click(object sender, EventArgs e)
    {
        string Rno      = "001";
        string Rname    = RnameTex.Text;
        string Rphone    = PhoneTex.Text;
        string Radr         = AdrTex.Text;
        string Rbrief   = BriefTex.Text;

        if (checkData(Rno, Rname, Rphone, Radr, Rbrief) == false)
        {
            Response.Write(@"<script>alert('信息填写不符合要求！');</script>");
            return;
        }
        string connectionString = WebConfigurationManager.ConnectionStrings["ConnectionStrBoss"].ConnectionString;
        using (SqlConnection conn = new SqlConnection(connectionString))
        {
            try
            {
                conn.Open();
                //String sqlString = "select * from Customer_Account where Aaccount = @username";
                String sqlString = "update Boss_Rest set Rno = @Rno  , Rname= @Rname,  Rphone = @Rphone,Radr = @Radr , Rbrief = @Rbrief";
                SqlParameter[] parameters = { 
                                                new SqlParameter("Rno   ", Rno),
                                                new SqlParameter("Rname ", Rname),
                                                new SqlParameter("Rphone", Rphone),                                                
                                                new SqlParameter("Radr  ", Radr),
                                                new SqlParameter("Rbrief", Rbrief)
                                            };
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sqlString;
                    cmd.Parameters.AddRange(parameters);
                    int ret = cmd.ExecuteNonQuery();
                    if (ret > 0)
                    {
                        Response.Write(@"<script>alert('修改成功');</script>");
                        Response.AddHeader("Refresh", "0");  //刷新当前页面
                    }
                    else
                    {
                        Response.Write(@"<script>alert('修改失败');</script>");
                    }
                }

            }
            catch (Exception ez)
            {
                Response.Write(@"<b>异常信息:</b>" + ez.Message);
            }
            finally
            {
                conn.Close();
            }
        }
    }
    private bool checkData(string Rno, string Rname, string Rphone, string Radr,string Rbrief)
    {
        bool ans = true;
        if (Rname.Length > 35 || Rname.Length ==0)
            ans = false;
        else if (Rphone.Length > 11 || Rphone.Length == 0)
            ans = false;
        else if (Radr.Length > 100 || Radr.Length ==0)
            ans = false;
        else if (Rbrief.Length > 200)
            ans = false;
        return ans;
    }
}